package com.cy.jt.securtiy.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Override
    public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {
            //USer user = userMapper.select
            if (!"jeck".equals(username))
                throw new UsernameNotFoundException("user not exists");
            String encodePwd = passwordEncoder.encode("123456");
            List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(
                    "ROLE_jinpai,POLE_tongpai,sys:res:retrieve,sys:res:create"
            );
            User user = new User(username,encodePwd,grantedAuthorities);
        System.out.println(user);
            return user;
        }
    }
